package com.gs.synedocket.db.sqlite;

import static com.gs.synedocket.constant.AllConstants.TAG;

import android.text.TextUtils;
import android.util.Log;

import com.gs.synedocket.R;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.db.bean.SyncBean;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.db.dto.CreateAddAcaDto;
import com.gs.synedocket.db.dto.DocketAccRepDto;
import com.gs.synedocket.db.dto.DocketAddChargeDto;
import com.gs.synedocket.db.dto.DocketAdditionalDto;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.db.dto.DocketQcDto;
import com.gs.synedocket.db.dto.MenuDto;
import com.gs.synedocket.db.dto.ShortDto;
import com.gs.synedocket.db.entity.Admixture;
import com.gs.synedocket.db.entity.Cement;
import com.gs.synedocket.db.entity.Customer;
import com.gs.synedocket.db.entity.Docket;
import com.gs.synedocket.db.entity.DocketAccRepPhoto;
import com.gs.synedocket.db.entity.DocketAdmixture;
import com.gs.synedocket.db.entity.DocketPhoto;
import com.gs.synedocket.db.entity.Mix;
import com.gs.synedocket.db.entity.ParamType;
import com.gs.synedocket.db.entity.Plant;
import com.gs.synedocket.db.entity.Product;
import com.gs.synedocket.db.entity.Site;
import com.gs.synedocket.db.entity.Team;
import com.gs.synedocket.db.entity.Truck;
import com.gs.synedocket.db.entity.UserTruck;
import com.gs.synedocket.db.query.DocketQuery;
import com.gs.synedocket.util.DateUtil;
import com.gs.synedocket.util.DocketUtil;
import com.gs.synedocket.util.GsonUtil;
import com.gs.synedocket.util.SPUtil;
import com.gs.synedocket.util.StringUtil;
import com.gs.synedocket.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;
import java.util.List;

public class DBApi {
    private static volatile DBApi instance;

    private DBApi() {
    }

    public static DBApi getInstance() {
        if (instance == null) {
            synchronized (DBApi.class) {
                if (instance == null) {
                    instance = new DBApi();
                }
            }
        }
        return instance;
    }

    public void setQueryCacheMap(String key, String value) {
        SPUtil.getInstance().putString(key, value);
    }

    public void setQueryCacheIds(String key, String ids) {
        String queryStr = getQueryCacheMap(key);
        SyncBean query = (SyncBean) GsonUtil.getInstance().fromJson(queryStr, SyncBean.class);
        query.setSynObjIds(ids);
        query.setOldTruckNo(query.getTruckNo());
        String json = GsonUtil.getInstance().toJson(query);
        setQueryCacheMap(key, json);
    }

    public void clearQueryCacheMap() {
        SPUtil.getInstance().putString(AllConstants.ON_HAND, null);
        SPUtil.getInstance().putString(AllConstants.ON_HAND_LIST, null);
        SPUtil.getInstance().putString(AllConstants.LOG_LIST, null);
    }

    public void setAllQueryCacheMap(String value) {
        setQueryCacheIds(AllConstants.ON_HAND, value);
        setQueryCacheIds(AllConstants.ON_HAND_LIST, value);
        setQueryCacheIds(AllConstants.LOG_LIST, value);
    }

    public String getQueryCacheMap(String key) {
        return SPUtil.getInstance().getString(key);
    }

    public void beginTransaction() {
        DBManager.getInstance().beginTransaction();
    }

    public void commitTransaction() {
        DBManager.getInstance().commitTransaction();
    }

    public void endTransaction() {
        DBManager.getInstance().endTransaction();
    }

    public void insertAdmixture(List<Admixture> list, String docketId) {
        if (list == null || list.isEmpty()) return;
        for (Admixture bean : list) {
            if (DBManager.getInstance().queryTableById(DBHelper.TABLE_ADMIXTURE, bean.getId()) > 0) {
                DBManager.getInstance().updateAdmixture(bean);
            } else {
                DBManager.getInstance().insertAdmixture(bean);
            }
            DocketAdmixture docketAdmixture = new DocketAdmixture();
            docketAdmixture.setAdmixtureId(bean.getId());
            docketAdmixture.setDocketId(docketId);
            DBManager.getInstance().insertDocketAdmixture(docketAdmixture);
        }
    }

    public void insertCement(Cement bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_CEMENT, bean.getId()) > 0) {
            DBManager.getInstance().updateCement(bean);
        } else {
            DBManager.getInstance().insertCement(bean);
        }
    }

    public void insertCustomer(Customer bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_CUSTOMER, bean.getId()) > 0) {
            DBManager.getInstance().updateCustomer(bean);
        } else {
            DBManager.getInstance().insertCustomer(bean);
        }
    }

    public void insertMix(Mix bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_MIX, bean.getId()) > 0) {
            DBManager.getInstance().updateMix(bean);
        } else {
            DBManager.getInstance().insertMix(bean);
        }
    }

    public void insertPlant(Plant bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_PLANT, bean.getId()) > 0) {
            DBManager.getInstance().updatePlant(bean);
        } else {
            DBManager.getInstance().insertPlant(bean);
        }
    }

    public List<ShortDto> queryPlant() {
        return DBManager.getInstance().queryPlantAsShort();
    }

    public void insertParam(List<ParamType> list) {
        if (list == null || list.isEmpty()) return;
        try {
            beginTransaction();
            for (ParamType bean : list) {
                bean.setUpload(1);
                DBManager.getInstance().insertParamType(bean);
            }
            commitTransaction();
        } finally {
            endTransaction();
        }
    }

    public List<ShortDto> queryParam() {
        return DBManager.getInstance().queryParamTypeAsShort();
    }

    public void insertProduct(Product bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_PRODUCT, bean.getId()) > 0) {
            DBManager.getInstance().updateProduct(bean);
        } else {
            DBManager.getInstance().insertProduct(bean);
        }
    }

    public void insertSite(Site bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_SITE, bean.getId()) > 0) {
            DBManager.getInstance().updateSite(bean);
        } else {
            DBManager.getInstance().insertSite(bean);
        }
    }

    public List<ShortDto> querySite() {
        return DBManager.getInstance().querySiteAsShort();
    }

    public void insertTeam(Team bean) {
        if (bean == null) return;
        if (DBManager.getInstance().queryTableById(DBHelper.TABLE_TEAM, bean.getId()) > 0) {
            DBManager.getInstance().updateTeam(bean);
        } else {
            DBManager.getInstance().insertTeam(bean);
        }
    }

    public void insertTruck(List<Truck> list) {
        if (list == null || list.isEmpty()) return;
        try {
            beginTransaction();
            for (Truck bean : list) {
                bean.setUpload(1);
                DBManager.getInstance().insertTruck(bean);
            }
            commitTransaction();
        } finally {
            endTransaction();
        }

    }

    public List<ShortDto> queryTruck() {
        return DBManager.getInstance().queryTruckAsShort();
    }

    public List<ShortDto> queryUserTruck(String userId) {
        return DBManager.getInstance().queryUserTruckAsShort();
    }

    public List<UserTruck> queryUserTruck() {
        return DBManager.getInstance().queryUserTruck(null);
    }

    public void insertUserTruck(List<UserTruck> list) {
        if (list == null || list.isEmpty()) return;
        try {
            beginTransaction();
            for (UserTruck bean : list) {
                bean.setUpload(1);
                DBManager.getInstance().insertUserTruck(bean);
            }
            commitTransaction();
        } finally {
            endTransaction();
        }
    }

    public void updateUserTruck(UserTruck bean, String oldTruckId) {
        UserTruck oldBean = new UserTruck();
        oldBean.setUserId(bean.getUserId());
        oldBean.setTruckId(oldTruckId);
        oldBean.setDef(false);
        updateUserTruck(oldBean, 1);
        bean.setDef(true);
        updateUserTruck(bean, 0);
    }

    public void updateUserTruck(UserTruck bean, int upload) {
        bean.setUpload(upload);
        DBManager.getInstance().updateUserTruck(bean);
    }

    public void insertDocket(Docket bean) {
        bean.setUpload(1);
        DBManager.getInstance().insertDocket(bean);
    }

    public String insertDockets(List<Docket> data, String postTruckNo) {
        StringBuilder ids = new StringBuilder();
        try {
            beginTransaction();
            for (Docket bean : data) {
                if (bean == null) continue;
                String id = bean.getId();
                ids.append(",").append(id);
                DocketDto docket = DBManager.getInstance().queryShortDocketById(id);
                if (bean.getChangeTruck() != null && bean.getChangeTruck().equalsIgnoreCase("XX")) {
                    if (docket != null && !postTruckNo.equals(bean.getTruckNo()) && !docket.getTruckNo().equals(bean.getTruckNo())) {
                        deleteOldDataById(id);
                        String docketId = DocketUtil.getInstance().getDocketId();
                        if (!TextUtils.isEmpty(docketId) && docketId.equals(id)) {
                            EventBus.getDefault().post(new Event(EventCode.EVENT_CHANGE_TRUCK));
                        }
                    }
                    continue;
                }
                // 判断本地的docket状态是否与服务器不同，可能docket变成作废单
                if (docket != null) {
                    if (bean.getState() == 6 && !docket.getState().equals(bean.getState())){
                        docket.setState(bean.getState());
                        DBManager.getInstance().updateDocketState(docket);
                        String docketId = DocketUtil.getInstance().getDocketId();
                        if (!TextUtils.isEmpty(docketId) && docketId.equals(id)) {
                            EventBus.getDefault().post(new Event(EventCode.EVENT_VOID));
                        }
                    }
                    continue;
                }
                insertAdmixture(bean.getAdmixtures(), id);
                insertCement(bean.getCement());
                insertCustomer(bean.getCustomer());
                insertMix(bean.getMix());
                insertPlant(bean.getPlant());
                insertProduct(bean.getProduct());
                insertSite(bean.getSite());
                insertTeam(bean.getTeam());
                insertAccRep(bean.getDocketAccRep());
                insertAddCharge(bean.getDocketAddCharge());
                insertAdditional(bean.getDocketAdditional());
                insertDocketPhoto(bean.getDocketPhotos());
                insertDocketQc(bean.getDocketQc());
                insertDocket(bean);
            }
            commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            endTransaction();
        }
        return ids.length() > 0 ? ids.substring(1) : "";
    }

    public DocketDto queryDocketById(String docketId) {
        return DBManager.getInstance().queryDocketById(docketId);
    }

    public DocketDto queryDocketByNo(String docketNo) {
        return DBManager.getInstance().queryDocketByNo(docketNo);
    }

    public DocketDto queryDocketOnHand(String docketId, String truckNo) {
        return DBManager.getInstance().queryDocketOnHand(docketId, truckNo);
    }

    public DocketDto queryDocketScanPaper(String docketNo) {
        return DBManager.getInstance().queryDocketByDocketNo(docketNo);
    }

    public DocketDto queryDocketLogById(String docketId) {
        return DBManager.getInstance().queryDocketLogById(docketId);
    }

    public int queryDocketTodayCount(String truckNo) {
        String today = DateUtil.DateTimeToStr(new Date());
        String startAt = today + " 00:00:00";
        String endAt = today + " 23:59:59";
        return DBManager.getInstance().queryDocketCountByAPAt(null, startAt, endAt, truckNo, null, null, "docketAt");
    }

    public int queryDocketMonthCount(String truckNo) {
        String startAt = DateUtil.getBeginDayOfMonth();
        String endAt = DateUtil.getEndDayOfMonth();
        return DBManager.getInstance().queryDocketCountByAPAt(null, startAt, endAt, truckNo, null, null, "docketAt");
    }

    public int queryDocketOnHandCount(String truckNo) {
        return DBManager.getInstance().queryDocketCountByDocketAt(truckNo);
    }

    public Docket queryDocketSyncList() {
        return DBManager.getInstance().queryUnUploadDocket();
    }

    public void updateDocketUpload(Docket bean) {
        DBManager.getInstance().updateUpload(DBHelper.TABLE_DOCKET, bean.get_id(), 1);
    }

    public List<DocketDto> queryDocketOnHandList(String truckNo, int offset, int limit) {
        return DBManager.getInstance().queryDocketListByDocketAt(truckNo, offset + "," + limit);
    }

    public int queryDocketLogCount(DocketQuery query, String what) {
        return DBManager.getInstance().queryDocketCountByAPAt(query.getDriverJobState(), query.getStartAt(), query.getEndAt(), query.getTruckNo(),
                query.getPlantId(), query.getSiteId(), what);
    }

    public List<DocketDto> queryDocketLogList(DocketQuery query, int offset, int limit, String what) {
        return DBManager.getInstance().queryDocketListByAPAt(query.getDriverJobState(), query.getStartAt(), query.getEndAt(), query.getTruckNo(),
                query.getPlantId(), query.getSiteId(), what, offset + "," + limit);
    }

    public List<DocketDto> queryDocketByCreateAt(int day) {
        String date = DateUtil.getFrontDate(new Date(), day);
        Log.i(TAG, "clearOldData: " + day + "天前的日期：" + date);
        return DBManager.getInstance().queryDocketByCreateAt(date);
    }

    public List<String> queryDocketListId(String truckNo) {
        return DBManager.getInstance().queryDocketListId("!=", truckNo);
    }

    public List<String> queryDocketLogId(String truckNo) {
        return DBManager.getInstance().queryDocketListId("=", truckNo);
    }

    public Date queryDocketModifyAt(int id) {
        return DBManager.getInstance().queryDocketModifyAt(id);
    }

    public void deleteOldData(String filterTime) {
        try {
            beginTransaction();
            DBManager.getInstance().deleteOldDocketPhoto(filterTime);
            DBManager.getInstance().deleteOldAddCharge(filterTime);
            DBManager.getInstance().deleteOldAdditional(filterTime);
            DBManager.getInstance().deleteOldDocketAdmixture(filterTime);
            DBManager.getInstance().deleteOldQc(filterTime);
            DBManager.getInstance().deleteOldAccRepPhotos(filterTime);
            DBManager.getInstance().deleteOldAccRep(filterTime);
            DBManager.getInstance().deleteOldDocket(filterTime);
            Log.i(TAG, "clearOldData: 删除数据成功");
            LogUtil.getInstance().insertOrReplaceData("删除3个月前的数据成功");
            commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getInstance().insertOrReplaceData("删除3个月前的数据失败：" + e);
        } finally {
            endTransaction();
        }
    }

    public void deleteOldDataById(String docketId) {
        try {
            beginTransaction();
            DBManager.getInstance().deleteDocketPhoto(docketId);
            DBManager.getInstance().deleteAddChargeByDocketId(docketId);
            DBManager.getInstance().deleteAdditionalByDocketId(docketId);
            DBManager.getInstance().deleteDocketAdmixtureById(docketId);
            DBManager.getInstance().deleteQcByDocketId(docketId);
            DBManager.getInstance().deleteOldAccRepPhotos(docketId);
            DocketAccRepDto accRepDto = DBManager.getInstance().queryAccRepByDocketId(docketId);
            if (accRepDto != null) {
                DBManager.getInstance().deleteAccRepPhoto(accRepDto.getId());
                DBManager.getInstance().deleteAccRepByDocketId(docketId);
            }
            DBManager.getInstance().deleteDocket(docketId);
            commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            endTransaction();
        }
    }

    public void updateDocketTruckState(String docketId, int truckState) {
        DocketDto bean = queryDocketById(docketId);
        if (bean == null) {
            ToastUtil.showToast(R.string.empty_docket);
            return;
        }
        if (truckState == 0) {
            bean.setArriveSiteAt(null);
            bean.setArriveSiteGps(null);
            bean.setArriveSiteGpsLoc(null);
        } else if (truckState == 1) {
            bean.setStartDisAt(null);
            bean.setStartDisGps(null);
            bean.setStartDisGpsLoc(null);
            bean.setEndDisAt(null);
            bean.setEndDisGps(null);
            bean.setEndDisGpsLoc(null);
            bean.setState(1);
            bean.setDriverRemark(null);
            DBManager.getInstance().deleteDocketPhoto(docketId);
            DBManager.getInstance().deleteAdditionalByDocketId(docketId);
            DBManager.getInstance().deleteAddChargeByDocketId(docketId);
            DBManager.getInstance().deleteQcByDocketId(docketId);
            DocketAccRepDto accRepDto = DBManager.getInstance().queryAccRepByDocketId(docketId);
            if (accRepDto != null) {
                DBManager.getInstance().deleteAccRepPhoto(accRepDto.getId());
                DBManager.getInstance().deleteAccRepByDocketId(docketId);
                DocketUtil.getInstance().setAccRep(null);
            }
        } else if (truckState == 2) {
            bean.setEndDisAt(null);
            bean.setEndDisGps(null);
            bean.setEndDisGpsLoc(null);
        }
        bean.setTruckState(truckState);
        bean.setUpload(0);
        DBManager.getInstance().updateDocket(bean);
    }

    public void updateDocketState(String docketId, int state) {
        updateDocketState(docketId, state, 0);
    }

    public void updateDocketState(String docketId, int state, int upload) {
        DocketDto bean = new DocketDto();
        bean.setId(docketId);
        bean.setState(state);
        bean.setUpload(upload);
        DBManager.getInstance().updateDocketState(bean);
    }

    public void updateDocketUpload(String docketId, int upload) {
        DocketDto bean = queryDocketById(docketId);
        if (bean == null) {
            ToastUtil.showToast(R.string.empty_docket);
            return;
        }
        bean.setUpload(upload);
        DBManager.getInstance().updateDocket(bean);
    }

    public void updateDocketComment(String docketId, String driverRemark) {
        DocketDto bean = queryDocketById(docketId);
        if (bean == null) {
            ToastUtil.showToast(R.string.empty_docket);
            return;
        }
        bean.setDriverRemark(driverRemark);
        bean.setUpload(0);
        DBManager.getInstance().updateDocket(bean);
    }

    public boolean updateDocketScanPhoto(String docketNo, String scanPhoto) {
        DocketDto bean = queryDocketByNo(docketNo);
        if (bean == null) {
            ToastUtil.showToast(R.string.empty_docket);
            return false;
        }
        bean.setScanPhoto(scanPhoto);
        bean.setState(2);
        bean.setTruckState(5);
        bean.setUpload(0);
        DBManager.getInstance().updateDocket(bean);
        return true;
    }

    public void updateDocketScanPhoto(Docket bean) {
        DBManager.getInstance().updateDocketScanPhoto(bean);
    }

    public void updateDocketSign(String docketId, int state, int truckState,
                                 Date date, String gps, String gpsLoc) {
        DocketDto bean = queryDocketById(docketId);
        if (bean == null) {
            ToastUtil.showToast(R.string.empty_docket);
            return;
        }
        bean.setState(state);
        bean.setTruckState(truckState);
        switch (truckState) {
            case 1:
                bean.setArriveSiteAt(date);
                bean.setArriveSiteGps(gps);
                bean.setArriveSiteGpsLoc(gpsLoc);
                break;
            case 2:
                bean.setStartDisAt(date);
                bean.setStartDisGps(gps);
                bean.setStartDisGpsLoc(gpsLoc);
                break;
            case 3:
                bean.setEndDisAt(date);
                bean.setEndDisGps(gps);
                bean.setEndDisGpsLoc(gpsLoc);
                break;
            case 4:
                bean.setLeaveSiteAt(date);
                bean.setLeaveSiteGps(gps);
                bean.setLeaveSiteGpsLoc(gpsLoc);
                break;
            case 5:
                bean.setArrivePlantAt(date);
                bean.setArrivePlantGps(gps);
                bean.setArrivePlantGpsLoc(gpsLoc);
                break;
        }
        bean.setUpload(0);
        DBManager.getInstance().updateDocket(bean);
    }

    public void insertAccRep(DocketAccRepDto bean) {
        if (bean == null) return;
        DocketAccRepDto old = DBManager.getInstance().queryAccRepByDocketId(bean.getDocketId());
        bean.setUpload(1);
        if (old == null) {
            DBManager.getInstance().insertAccRep(bean);
        } else {
            DBManager.getInstance().deleteAccRepPhoto(old.getId());
            DBManager.getInstance().updateAccRepByDocketId(bean);
        }
        if (bean.getDocketAccRepPhotos() != null && !bean.getDocketAccRepPhotos().isEmpty()) {
            for (DocketPhoto photoDto : bean.getDocketAccRepPhotos()) {
                DocketAccRepPhoto photo = new DocketAccRepPhoto();
                photo.setAccRepId(bean.getId());
                photo.setPhoto(photoDto.getPhoto());
                photo.setUpload(1);
                DBManager.getInstance().insertAccRepPhoto(photo);
            }
        }
    }

    public void updateAccRepPhoto(DocketAccRepPhoto bean) {
        DBManager.getInstance().updateAccRepPhoto(bean);
    }

    public void insertNewAccRep(DocketAccRepDto bean) {
        DocketAccRepDto old = DBManager.getInstance().queryAccRepByDocketId(bean.getDocketId());
        bean.setUpload(0);
        String accRepId;
        if (old == null) {
            accRepId = StringUtil.getGUID();
            bean.setId(accRepId);
            DBManager.getInstance().insertAccRep(bean);
        } else {
            accRepId = old.getId();
            DBManager.getInstance().deleteAccRepPhoto(accRepId);
            DBManager.getInstance().updateAccRepByDocketId(bean);
        }
        if (bean.getDocketAccRepPhotos() != null && !bean.getDocketAccRepPhotos().isEmpty()) {
            for (DocketPhoto photoDto : bean.getDocketAccRepPhotos()) {
                String photo1 = photoDto.getPhoto();
                DocketAccRepPhoto photo = new DocketAccRepPhoto();
                photo.setAccRepId(accRepId);
                photo.setPhoto(photo1);
                photo.setUpload(StringUtil.isLocalPhoto(photo1) ? 0 : 1);
                DBManager.getInstance().insertAccRepPhoto(photo);
            }
        }
    }

    public void updateAccRepUpload(DocketAccRepDto bean) {
        DBManager.getInstance().updateUpload(DBHelper.TABLE_DOCKET_ACC_REP, bean.get_id(), 1);
    }

    public void insertAddCharge(DocketAddChargeDto bean) {
        if (bean == null) return;
        bean.setUpload(1);
        DBManager.getInstance().insertAddCharge(bean);
    }

    public void insertNewAddCharge(DocketAddChargeDto bean) {
        bean.setId(StringUtil.getGUID());
        bean.setUpload(0);
        DBManager.getInstance().insertAddCharge(bean);
    }

    public void updateDocketAddChargePhoto(DocketAddChargeDto bean) {
        DBManager.getInstance().updateAddChargePhoto(bean);
    }

    public void updateDocketAddChargeUpload(DocketAddChargeDto bean) {
        DBManager.getInstance().updateUpload(DBHelper.TABLE_DOCKET_ADD_CHARGE, bean.get_id(), 1);
    }

    public void insertAdditional(DocketAdditionalDto bean) {
        if (bean == null) return;
        bean.setUpload(1);
        DBManager.getInstance().insertAdditional(bean);
    }

    public void insertAddAca(CreateAddAcaDto bean) {
        String docketId = bean.getDocketId();
        // 修改comment
        updateDocketComment(docketId, bean.getDriverRemark());
        // 修改comment photo
        DBManager.getInstance().deleteDocketPhoto(docketId);
        if (bean.getPhotos() != null && !bean.getPhotos().isEmpty()) {
            for (DocketPhoto photoDto : bean.getPhotos()) {
                photoDto.setDocketId(docketId);
                photoDto.setUpload(StringUtil.isLocalPhoto(photoDto.getPhoto()) ? 0 : 1);
                DBManager.getInstance().insertDocketPhoto(photoDto);
            }
        }
        // 修改additional
        DocketAdditionalDto old = DBManager.getInstance().queryAdditionalByDocketId(docketId);
        DocketAdditionalDto additional = bean.getAdditional();
        additional.setUpload(0);
        if (old == null) {
            additional.setId(StringUtil.getGUID());
            DBManager.getInstance().insertAdditional(additional);
        } else {
            DBManager.getInstance().updateAdditional(additional);
        }
    }

    public void updateAdditionalUpload(DocketAdditionalDto bean) {
        DBManager.getInstance().updateUpload(DBHelper.TABLE_DOCKET_ADDITIONAL, bean.get_id(), 1);
    }

    public void insertDocketPhoto(List<DocketPhoto> list) {
        if (list == null || list.isEmpty()) return;
        for (DocketPhoto bean : list) {
            bean.setUpload(1);
            DBManager.getInstance().insertDocketPhoto(bean);
        }
    }

    public void updateDocketPhoto(DocketPhoto bean) {
        DBManager.getInstance().updateDocketPhoto(bean);
    }

    public void updateDocketPhotoUpload(String docketId) {
        DBManager.getInstance().updateDocketPhoto(docketId, 1);
    }

    public void insertDocketQc(DocketQcDto bean) {
        if (bean == null) return;
        bean.setUpload(1);
        DBManager.getInstance().insertDocketQc(bean);
    }

    public void insetDocketQc(DocketQcDto bean) {
        bean.setId(StringUtil.getGUID());
        bean.setUpload(0);
        DBManager.getInstance().insertDocketQc(bean);
    }

    public void updateDocketQcPhoto(DocketQcDto bean) {
        DBManager.getInstance().updateDocketQc(bean);
    }

    public void updateDocketQcUpload(DocketQcDto bean) {
        DBManager.getInstance().updateUpload(DBHelper.TABLE_DOCKET_QC, bean.get_id(), 1);
    }

    public void insertMenu(List<MenuDto> list) {
        if (list == null || list.isEmpty()) return;
        try {
            beginTransaction();
            for (MenuDto bean : list) {
                bean.setUpload(1);
                DBManager.getInstance().insertMenu(bean);
            }
            commitTransaction();
        } finally {
            endTransaction();
        }
    }

    public List<MenuDto> queryMenu(String pid, String quick) {
        return DBManager.getInstance().queryMenu(pid.toLowerCase(), quick, "App");
    }

    public void addLogoutMenu() {
        MenuDto bean = new MenuDto();
        bean.setId("c081f0c8-bec5-4f3b-86e6-a28de5000000");
        bean.setpId("c081f0c8-bec5-4f3b-86e6-a28bf5068286");
        bean.setTypeName("menu");
        bean.setEnName("Logout");
        bean.setCnName("登出");
        bean.setAppAct("exitSysAct");
        bean.setState(true);
        bean.setOperFlag(1);
        bean.setOrderId(7);
        bean.setGpFlag(false);
        bean.setViewFlag("App");
        bean.setQuickFlag("left");
        DBManager.getInstance().insertMenu(bean);
    }

    public void deleteUserTruckTable() {
        DBManager.getInstance().deleteTableAndSequence(DBHelper.TABLE_USER_TRUCK);
    }

    public void deleteAll() {
        try {
            beginTransaction();
            DBManager.getInstance().delete(DBHelper.TABLE_ADMIXTURE);
            DBManager.getInstance().delete(DBHelper.TABLE_CEMENT);
            DBManager.getInstance().delete(DBHelper.TABLE_CUSTOMER);
            DBManager.getInstance().delete(DBHelper.TABLE_MIX);
            DBManager.getInstance().delete(DBHelper.TABLE_PLANT);
            DBManager.getInstance().delete(DBHelper.TABLE_PRODUCT);
            DBManager.getInstance().delete(DBHelper.TABLE_SITE);
            DBManager.getInstance().delete(DBHelper.TABLE_TEAM);
            DBManager.getInstance().delete(DBHelper.TABLE_TRUCK);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_ACC_REP);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_ACC_REP_PHOTO);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_ADD_CHARGE);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_ADDITIONAL);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_ADMIXTURE);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_PHOTO);
            DBManager.getInstance().delete(DBHelper.TABLE_DOCKET_QC);
            DBManager.getInstance().delete(DBHelper.TABLE_PARAM_TYPE);
            DBManager.getInstance().delete(DBHelper.TABLE_MENU);
            DBManager.getInstance().delete(DBHelper.TABLE_USER_TRUCK);
            DBManager.getInstance().delete(DBHelper.SYS_TABLE_SEQUENCE);
            commitTransaction();
        } catch (Exception ignored) {

        } finally {
            endTransaction();
        }
    }

    public void deleteSysTable() {
        try {
            beginTransaction();
            DBManager.getInstance().deleteTableAndSequence(DBHelper.TABLE_TRUCK);
            DBManager.getInstance().deleteTableAndSequence(DBHelper.TABLE_PARAM_TYPE);
            DBManager.getInstance().deleteTableAndSequence(DBHelper.TABLE_MENU);
            DBManager.getInstance().deleteTableAndSequence(DBHelper.TABLE_USER_TRUCK);
            commitTransaction();
        } catch (Exception ignored) {

        } finally {
            endTransaction();
        }
    }
}
